/*
============================================================================
 Name        : GetImei.cpp
 Author      : wwk
 Version     :
 Copyright   : Your copyright notice
 Description :  
============================================================================
*/

#include "getimei.h"
#if !defined(EKA2)
	#include <plpvariant.h>
	#include <plpvar.h>	
#endif

CGetIMEI* CGetIMEI::NewL()
{
	CGetIMEI* self = new (ELeave) CGetIMEI();
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(self);

	return self;
}

CGetIMEI::CGetIMEI() : CActive(EPriorityHigh)
{
#if defined(EKA2)
	iTelephony = NULL;
#endif
	iIMEI.Zero();
	iModel.Zero();
	iState = EStart;
}

void CGetIMEI::ConstructL()
{
#if defined(EKA2)
	iTelephony = CTelephony::NewL();
#endif
	CActiveScheduler::Add(this);

}

CGetIMEI::~CGetIMEI()
{
	Cancel(); 
#if defined(EKA2)
	if(iTelephony)
	{
		delete iTelephony;
		iTelephony = NULL;
	}
#endif
}

void CGetIMEI::DoCancel()
{
#if defined(EKA2)
	iTelephony->CancelAsync(CTelephony::EGetPhoneIdCancel);
#endif
}

void CGetIMEI::StartL()
{
#if defined(EKA2)
	iIMEI.SetLength(iIMEI.MaxLength());
	iIMEI.FillZ();
	iIMEI.Zero();

	iModel.SetLength(iModel.MaxLength());
	iModel.FillZ();
	iModel.Zero();
	Cancel();
	iState = EGetPhoneInfo;
	CTelephony::TPhoneIdV1Pckg phoneIdPckg( iPhoneId ); 
	iTelephony->GetPhoneId(iStatus, phoneIdPckg);
	SetActive();
	iActiveSchedulerWait.Start();
#endif
}

void CGetIMEI::RunL()
{
#if defined(EKA2)
	if ( iActiveSchedulerWait.IsStarted() )
	{
		iActiveSchedulerWait.AsyncStop();
		if(iStatus == KErrNone)
		{
			iState = EDone;
			iIMEI.Copy(iPhoneId.iSerialNumber);	
			iModel.Copy(iPhoneId.iManufacturer);
			iModel.Append(iPhoneId.iModel);
		}
		else
		{
			iState = EGetPhoneInfoErr;
			//iIMEI.Num(iStatus.Int());
			//iModel.Num(iStatus.Int());
			iIMEI.SetLength(0);
			iModel.SetLength(0);
			iState = EGetPhoneInfoErr ;
		}			
	}
#endif
}
const TPtrC CGetIMEI::GetIMEI()
{
#if defined(EKA2)
	StartL();
	TPtrC ptr(iIMEI);
	return ptr;
#else
	iIMEI.SetLength(iIMEI.MaxLength());
	iIMEI.FillZ();
	iIMEI.Zero();

	iModel.SetLength(iModel.MaxLength());
	iModel.FillZ();
	iModel.Zero();

	#if !defined(__WINS__)
		TPlpVariantMachineId aId;
		aId.SetLength(aId.MaxLength());
		aId.Fill(0);
		aId.Zero();
		PlpVariant::GetMachineIdL( aId );
		if(aId.Length() > 0)
		{
			iIMEI.Copy(aId);
		}
		iModel.Copy(_L("KMachineUid"));
	#else
		iModel.Copy(_L("emulator"));
		iIMEI.Copy(_L("emulator"));
	#endif
	TPtrC ptr(iIMEI.Ptr());
	iState = EDone;
	return ptr;
#endif
}
const TPtrC CGetIMEI::GetModel()
{
#if defined(EKA2)
	StartL();
	TPtrC ptr(iModel.Ptr());
	return ptr;
#else
	iIMEI.SetLength(iIMEI.MaxLength());
	iIMEI.FillZ();
	iIMEI.Zero();

	iModel.SetLength(iModel.MaxLength());
	iModel.FillZ();
	iModel.Zero();

#if !defined(__WINS__)
	TPlpVariantMachineId aId;
	aId.SetLength(aId.MaxLength());
	aId.Fill(0);
	aId.Zero();
	PlpVariant::GetMachineIdL( aId );
	if(aId.Length() > 0)
	{
		iIMEI.Copy(aId);
	}
	iModel.Copy(_L("KMachineUid"));
#else
	iIMEI.Copy(_L("emulator"));
	iModel.Copy(_L("emulator"));
#endif
	TPtrC ptr(iModel.Ptr());
	iState = EDone;
	return ptr;
#endif
}
TInt CGetIMEI::GetState()
{
	return iState;
}
